package com.sinaapp.gavinzhang.ConcurrentProgramming;

/**
 * interrupted方法是Thread静态方法
 * interrupt方法只是改变线程状态，且只有在sleep，wait，join时会打断阻塞。
 * isInterrupted方法用来判断线程状态是否被中止
 *
 * Created by gavin on 15-9-1.
 */
public class InterruptTest {
    public static void main(String[] args)
    {
        Object object = new Object();
        Circulate circulate = new Circulate();
        circulate.object = object;
        Thread thread = new Thread(circulate);

        /**线程被阻塞在同步块外
        synchronized (object)
        {
            thread.start();
            System.out.println(thread.getState());
            while (true)
            {
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                System.out.println(thread.getState());
            }
        }
         */
        System.out.println(thread.getState());
        thread.start();
        thread.interrupt();

        System.out.println(thread.isInterrupted() + " " + thread.getState());
        System.out.println(thread.isAlive());
    }
}


class Circulate implements Runnable{
    public Object object;
    public void run() {

        synchronized (object){
            System.out.println("enter synchronized");
        }
        while (true)
        {
            System.out.println(Thread.currentThread().getName()+"  "+System.currentTimeMillis());
            long start = System.currentTimeMillis();
            //while (System.currentTimeMillis()-start<1000);
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }
}